function GetQueryString(name)
{
 var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
 var r = window.location.search.substr(1).match(reg);
 if(r!=null)return  unescape(r[2]); return null;
}

function getAllblogs() {
	var pageCount= GetQueryString("pageCount") ;
	var limitCount = 9;
		$.ajax({
			type : "POST",
			url : "blog_requestAllBlogs.action",
			data : {page:pageCount == undefined?1:pageCount,limit:limitCount,length:20},
			dataType : 'json',
			success : function(result) {
			var	datas = result.data.list;
			if(result.data.allRow >limitCount){
				setPageParam(result.data);
			}
				var i = 0;
				for (i = 0; i < datas.length; i++) {
					var tr = $("<tr></tr>");
					tr.appendTo($("#blogList"));
					var id = $("<td width='5%'>" + (i+1)
							+ "</td>");
					var kind = $("<td width='10%'>" + datas[i].kind
							+ "</td>");
					var title = $("<td width='25%'>" + datas[i].title
							+ "</td>");
					var pubdate = $("<td width='12%'>"
							+ datas[i].pubdate + "</td>");
					var author = $("<td width='12%'>"
							+ datas[i].author + "</td>");
					var mainText = $("<td width='10%'>"
							+ datas[i].mainText + "</td>");
					var deal = $("<td width='26%'></td>");
					var edit = $("<a class='bj_btn' href='../addBlog.html?id="
							+ datas[i].id + "'>编辑</a>");
					var check = $("<a class='sj_btn' target='view_window'href='../single.html?id="
							+ datas[i].id + "'>查看</a>");
					var del = $("<a class='del_btn' name='del' onclick='onRemove(this);' data-id='"+datas[i].id+"' >删除</a>");
					
					edit.appendTo(deal);
					check.appendTo(deal);
					del.appendTo(deal);
					id.appendTo(tr);
					kind.appendTo(tr);
					title.appendTo(tr);
					pubdate.appendTo(tr);
					author.appendTo(tr);
					mainText.appendTo(tr);
					deal.appendTo(tr);
				};

			}
		});

	};
	function setPageParam(data) {
		var nextPager,
			prePager;
		if(data.hasNextPage){
			nextPager =$("<li class='next'><a href=blogManager.html?pageCount="+(data.currentPage+1)+" >下一页 &rarr;</a></li>");
		}else{
			nextPager =$("<li class='next disabled'><a >下一页 &rarr;</a></li>");
		}
		
		if(data.hasPreviousPage){
			prePager =$("<li class='previous'><a href=blogManager.html?pageCount="+(data.currentPage-1)+" >&larr; 上一页</a></li>");
		}else{
			prePager =$("<li class='previous disabled'><a >&larr; 上一页</a></li>");
		}
		nextPager.appendTo($("#pager"));
		prePager.appendTo($("#pager"));
		
	}
var onRemove = function(obj){
	var id=$(obj).attr('data-id');
		$.ajax({
			type : "GET",
			url : "dealBlog_removeBlogArticle.action",
			data : "id=" + id,
			dataType : 'html',
			success : function(result) {
				result = result.substring(1, result.length - 1);
				if (result == "删除成功") {
					window.location.reload();
				} else {
					alert(result);
				}

			}
			});
};
$(function() {
		getAllblogs();
});